<?php

namespace App\Model;

use Illuminate\Http\Request;
use mysql_xdevapi\Exception;

class AdminLog extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'admin_logs';

    const ACCOUNT = 'account';
    const BANNER = 'banner';
    const PERMISSION = 'permission';
    const GIFT = 'gift';
    const GOODS = 'goods';
    const NUMBER = 'number';
    const ROLE = 'role';
    const ROOM = 'room';
    const SYSTEM = 'system';
    const USER = 'user';
    const VERSION = 'version';
    const FEEDBACK = 'feedback';
    const CUSTOM_MSG = 'custom_msg';
    const FRIENDS_REPORT = 'friends_report';
    const ROOM_REPORT = 'room_report';
    const DIAMOND_RECORD = 'diamond_record';
    const SEND_GOODS_RECORD = 'send_goods_record';
    const TRENDS = 'trends';
    const GENERATION_CHARGE_RECORD = 'generation_charge_record';
    const FINANCE = 'finance';
    const UNION = 'union';
    const ORIGAMI = 'origamiTopics';
    const UNION_PAY_ORDER = 'unionPayOrder';
    const MALL_MAIN = 'MallMain';
    const FULL_SCREEN = 'FullScreen';

    private $fields = [ 'id','admin_name','path','method','ip','input','created_at' ];

    const CONTROLLER_MAP = [
        self::ACCOUNT => '账目管理',
        self::BANNER => '广告管理',
        self::PERMISSION => '权限管理',
        self::GIFT => '礼物管理',
        self::GOODS => '商品',
        self::NUMBER => '用户管理',
        self::ROLE => '角色管理',
        self::ROOM => '房间管理',
        self::SYSTEM => '系统管理',
        self::USER => '管理员',
        self::VERSION => '版本管理',
        self::FEEDBACK => '用户反馈',
        self::CUSTOM_MSG => '自定义消息',
        self::FRIENDS_REPORT => '用户举报',
        self::ROOM_REPORT => '房间举报',
        self::DIAMOND_RECORD => '扣款\赠送记录',
        self::SEND_GOODS_RECORD => '赠送礼物',
        self::TRENDS => '动态管理',
        self::GENERATION_CHARGE_RECORD => '代充',
        self::FINANCE => '财务管理',
        self::UNION => '公会管理',
        self::ORIGAMI => '折纸管理',
        self::UNION_PAY_ORDER => '银联支付订单管理',
        self::MALL_MAIN => '商城CMS管理',
        self::FULL_SCREEN => '飘萍管理',
    ];

    public static function createData(Request $request, $controller = '',$desc = '', $users=null){
        if('' === $desc || !isset(self::CONTROLLER_MAP[$controller])) throw new Exception('参数错误');
        if (!$users){
            $token = $request->header('x-token') ?? '';
            $users = Users::findByToken($token);
        }
        $adminLogModel = new AdminLog();
        $adminLogModel->admin_id = $users->id;
        $adminLogModel->admin_name = $users->name;
        $adminLogModel->path = self::CONTROLLER_MAP[$controller].':'.$desc;
        $adminLogModel->method = $request->method();
        $adminLogModel->ip = $request->ip();
        $adminLogModel->input = json_encode($request->all());
        return $adminLogModel->save();
    }

    public function getList(Request $request){
        $limit = $request->input('limit',10);
        $page = $request->input('page',1);
        $query = new AdminLog();
        // todo 搜索
        return $query->orderBy('id','DESC')->paginate($limit,$this->fields,'page',$page);
    }
}
